//
//  GFPandectCell.m
//  GoodFood
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFPandectCell.h"

@interface GFPandectCell ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *modelArr;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, strong) GFWaitView *waitView;
@property (nonatomic, strong) GFOverTimeView *overTimeView;
@end

@implementation GFPandectCell

#pragma mark _ 数据处理
- (void)handleData {
    
    self.modelArr = [NSMutableArray array];
    
    NSInteger a = self.itemNumber.integerValue;
    
    //    NSLog(@"%@", self.urlArr[a]);
    
    [AFNetHandle GETWithURL:self.urlArr[a] body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        NSArray *arr = [result objectForKey:@"foods"];
        
        for (NSDictionary *dic in arr) {
            
            GFPandectModel *panModel = [GFPandectModel modWithDic:dic];
            
            [self.modelArr addObject:panModel];
            
        }
        
        
        [self.tableView reloadData];
        if (arr.count != 0) {
            if (self.modelArr.count < 20) {
                self.tableView.footer.hidden = YES;
            } else {
                self.tableView.footer.hidden = NO;
            }
        } else {
            self.tableView.footer.hidden = YES;
        }
        [self.waitView removeFromSuperview];
        [self.overTimeView removeFromSuperview];
        
    } fail:^(NSError *error) {
        [self.overTimeView removeFromSuperview];
        self.overTimeView = [[GFOverTimeView alloc] initWithFrame:self.contentView.frame withButtonBlock:^(UIButton *button) {
            [self.contentView addSubview:self.waitView];
            [self handleData];
        }];
        [self.waitView removeFromSuperview];
        [self.contentView addSubview:self.overTimeView];
        
    }];
    
    
}



- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {

        self.pageNum = 2;
        
        self.tableView = [[UITableView alloc] initWithFrame:self.contentView.frame style:UITableViewStylePlain];
        
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        
        self.tableView.rowHeight = ScreenWidth / 375 * 100;
        
        [self.tableView registerClass:[GFPandectTableViewCell class] forCellReuseIdentifier:@"PandectTableViewCell"];
        
        [self.contentView addSubview:self.tableView];
        
        self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dragDown:)];
        self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(pushUp:)];
        self.tableView.footer.hidden = YES;
    }
    
    return self;
}

#warning 上拉加载
- (void)pushUp:(MJRefreshFooter *)footer {
    [self.contentView addSubview:self.waitView];
    NSInteger a = self.itemNumber.integerValue;
    
    NSArray *arr = [self.urlArr[a] componentsSeparatedByString:@"&"];
    
    NSString *string = [NSString stringWithFormat:@"%@&%@&%@&page=%ld&%@", arr[0], arr[1], arr[2], self.pageNum, arr[4]];
    
    [AFNetHandle GETWithURL:string body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        NSArray *arr = [result objectForKey:@"foods"];
        
        for (NSDictionary *dic in arr) {
            
            GFPandectModel *panModel = [GFPandectModel modWithDic:dic];
            
            [self.modelArr addObject:panModel];
            
        }
        
        [self.tableView reloadData];
        
        //        NSLog(@"%@", self.modelArr);
        
        [self.tableView.footer endRefreshing];
        if (arr.count != 0) {
            if (self.modelArr.count < 20) {
                self.tableView.footer.hidden = YES;
            } else {
                self.tableView.footer.hidden = NO;
            }
        } else {
            self.tableView.footer.hidden = YES;
        }
        [self.waitView removeFromSuperview];
        [self.overTimeView removeFromSuperview];
         self.pageNum++;
    } fail:^(NSError *error) {
        [self.overTimeView removeFromSuperview];
        self.overTimeView = [[GFOverTimeView alloc] initWithFrame:self.contentView.frame withButtonBlock:^(UIButton *button) {
            [self.contentView addSubview:self.waitView];
            [self pushUp:self.tableView.footer];
        }];
        [self.waitView removeFromSuperview];
        [self.contentView addSubview:self.overTimeView];
    }];
    
   
    
}

#warning 下拉刷新
- (void)dragDown:(MJRefreshHeader *)header {
    [self.contentView addSubview:self.waitView];
    [self.tableView.header beginRefreshing];
//    [self.tableView reloadData];
    [self.tableView.header endRefreshing];
    [self handleData];
    
}


#pragma mark _ tableDataSource 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GFPandectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PandectTableViewCell"];
    
    GFPandectModel *panModel = [self.modelArr objectAtIndex:indexPath.row];
    
    cell.panModel = panModel;
    
    cell.itemNumber = self.itemNumber;
    
    /* 尝试添加的动画  以后看看如何添加 */
#warning cell动画
//    cell.layer.transform = CATransform3DMakeScale(0.5, 0.5, 0.5);
//    [UIView animateWithDuration:0.5 animations:^{
//        cell.layer.transform = CATransform3DMakeTranslation(30, 30, 0);
//    }];
    
    return cell;
    
}

#pragma mark _ tableView Delegate 协议方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    GFPandectModel *panModel = [self.modelArr objectAtIndex:indexPath.row];
    
    self.clickBlock(panModel.code);
    
}



#pragma mark _ 属性的set
- (void)setItemNumber:(NSString *)itemNumber {
    if (_itemNumber != itemNumber) {

        _itemNumber = itemNumber;
    }
    self.waitView = [[GFWaitView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.contentView addSubview:self.waitView];
    [self handleData];
    
    //    NSLog(@"2----%@", self.itemNumber);
    
}

- (void)setUrlArr:(NSMutableArray *)urlArr {
    if (_urlArr != urlArr) {

        _urlArr = urlArr;
    }
    
    //    NSLog(@"1----%@", self.itemNumber);
}


@end
